<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsPurchaseOrderInfo extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['i.purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['i.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $delivery_time = $this->request->post('delivery_time');
            if(!empty($delivery_time)){
                $str = explode('~',$delivery_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['i.delivery_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $supplyer_code = $this->request->post('supplyer_code');
            if(!empty($supplyer_code)){
                $where['i.supplyer_code'] = $supplyer_code;
            }
            $goods_code = $this->request->post('goods_code');
            if(!empty($goods_code)){
                $where['i.goods_code'] = $goods_code;
            }
            $keyword = $this->request->post('keyword');
            if(!empty($keyword)){
                $where['i.purchase_code|i.supplyer_code|i.goods_code'] = $keyword;
            }
            //$where['po.examine_status'] = '1';

            $field = empty(input('field'))?'goods_code':trim(input('field'));
            $order = empty(input('sort'))?'DESC':trim(input('sort'));

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }
            if($pageStatus){
                $list = Db::name('kuyou_goods_purchase_order_info')->alias('i')
                    ->join('kuyou_goods_purchase_order po','po.purchase_code=i.purchase_code')
                    ->join('kuyou_goods g','g.goods_code=i.goods_code')
                    ->join('kuyou_unit u','u.id=i.default_unit')
                    ->join('kuyou_suppliers s','s.supplyer_code=i.supplyer_code')
                    ->join('kuyou_stores r','r.id=i.stores_id')
                    ->where($where)
                    ->page($page,$limit)
                    ->order(['i.'.$field=>$order,'i.purchase_time'=>'DESC'])
                    ->field('i.id,i.goods_code,i.purchase_code,i.purchase_type,FROM_UNIXTIME(i.purchase_time,\'%Y-%m-%d\') as purchase_time,FROM_UNIXTIME(i.delivery_time,\'%Y-%m-%d\') as delivery_time,
                i.supplyer_code,i.money_tax_price,po.status,i.number,i.tax_included,i.number as library_number,i.money_tax_price as library_money,FROM_UNIXTIME(i.library_time,\'%Y-%m-%d\') as library_time,
                g.goods_name,g.goods_specification_id as goods_spec,
                s.supplyer_code as s_code,s.supplyer_name as s_name,
                r.name as r_name,
                u.name as unitname')
                    ->select();
            }else{
                $list = Db::name('kuyou_goods_purchase_order_info')->alias('i')
                    ->join('kuyou_goods_purchase_order po','po.purchase_code=i.purchase_code')
                    ->join('kuyou_goods g','g.goods_code=i.goods_code')
                    ->join('kuyou_unit u','u.id=i.default_unit')
                    ->join('kuyou_suppliers s','s.supplyer_code=i.supplyer_code')
                    ->join('kuyou_stores r','r.id=i.stores_id')
                    ->where($where)
                    ->order(['i.'.$field=>$order,'i.purchase_time'=>'DESC'])
                    ->field('i.id,i.goods_code,i.purchase_code,i.purchase_type,FROM_UNIXTIME(i.purchase_time,\'%Y-%m-%d\') as purchase_time,FROM_UNIXTIME(i.delivery_time,\'%Y-%m-%d\') as delivery_time,
                i.supplyer_code,i.money_tax_price,po.status,i.number,i.tax_included,i.number as library_number,i.money_tax_price as library_money,FROM_UNIXTIME(i.library_time,\'%Y-%m-%d\') as library_time,
                g.goods_name,g.goods_specification_id as goods_spec,
                s.supplyer_code as s_code,s.supplyer_name as s_name,
                r.name as r_name,
                u.name as unitname')
                    ->select();
            }
            $row_count = Db::name('kuyou_goods_purchase_order_info')->alias('i')
                ->join('kuyou_goods_purchase_order po','po.purchase_code=i.purchase_code')
                ->join('kuyou_goods g','g.goods_code=i.goods_code')
                ->join('kuyou_unit u','u.id=i.default_unit')
                ->join('kuyou_suppliers s','s.supplyer_code=i.supplyer_code')
                ->join('kuyou_stores r','r.id=i.stores_id')
                ->where($where)->count();

            foreach ($list as $list_k=>$list_v){
                $purord_info = Db::name('kuyou_goods_purord_info')->where(['order_id'=>$list_v['id'],'status'=>'1'])->field('sum(number) as number,sum(money_tax_price) as all_money')->find();

                $p_id = Db::name('kuyou_goods_purord_info')->where(['order_id'=>$list_v['id'],'status'=>'1'])->value('purchase_id');
                $goods_excpur_info = Db::name('kuyou_goods_excpur_info')->where(['purchase_id'=>$p_id,'status'=>'1'])->find();
                if($goods_excpur_info){
                    $purord_info['number'] = round( ($purord_info['number']-$goods_excpur_info['number']),2 );
                    $purord_info['all_money'] = round( ($purord_info['all_money']-$goods_excpur_info['money_tax_price']),2 );
                }

                if($purord_info){
                    $list[$list_k]['library_number'] = round(($list[$list_k]['library_number']-$purord_info['number']),2);
                    $list[$list_k]['library_money'] = round(($list[$list_k]['library_money']-$purord_info['all_money']),4);
                }
                if($list[$list_k]['library_number']>0 && $list[$list_k]['library_number']<$list[$list_k]['number']){
                    $list[$list_k]['status'] = 2;
                }else if($list[$list_k]['library_number']>0 && $list[$list_k]['library_number']=$list[$list_k]['number']){
                    $list[$list_k]['status'] = 0;
                }
                $purord_time = Db::name('kuyou_goods_purord_info')->where(['order_id'=>$list_v['id'],'status'=>1])->order('times','DESC')->value('times');
                if($purord_time){
                    $list[$list_k]['library_time'] = date('Y-m-d',$purord_time);
                }
            }

            foreach ($list as $list_k=>$list_v){
                if($list_v['purchase_type']==2){
                    $list[$list_k]['number'] = -$list_v['number'];
                    $list[$list_k]['money_tax_price'] = -$list_v['money_tax_price'];
                    $list[$list_k]['library_number'] = -$list_v['library_number'];
                    $list[$list_k]['library_money'] = -$list_v['library_money'];
                }
            }

            $count = [
                'number'=>0,
                'money_tax_price'=>0,
                'library_number'=>0,
                'library_money'=>0,
            ];
            $counts = $count;
            $counts['purchase_type'] = '4';
            $data = [];
            $now = isset($list[0][$field])?$list[0][$field]:'';
            foreach ($list as $list_k=>$list_v){
                if($list_v[$field]!=$now){
                    $now = $list_v[$field];
                    $row = $count;$row['purchase_type'] = '3';
                    $data[] = $row;
                    foreach ($count as $count_k=>$count_v){
                        $count[$count_k] = $list_v[$count_k];
                        $counts[$count_k] += $list_v[$count_k];
                    }
                }else{
                    foreach ($count as $count_k=>$count_v){
                        $count[$count_k] += $list_v[$count_k];
                        $counts[$count_k] += $list_v[$count_k];
                    }
                }
                $data[] = $list_v;
                if($list_k==(sizeof($list)-1) || sizeof($list)==1){
                    $row = $count;$row['purchase_type'] = '3';
                    $data[] = $row;
                }
            }
            $data[] = $counts;
            $result = array("count" => $row_count, "list" => $data, "field" => $field, "order" => $order);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
